<template>
  <view>
  <view class="searchBox">
    <uni-search-bar @input="input" placeholder="搜索内容" :radius="100" cancelButton="none"></uni-search-bar>
  </view>
  
    <view class="searchbox-item" v-if="searList.length !==0">
        <view class="searchbox">
            <view class="searchList" v-for="(item,s) in searList" :key="s" @click="handlerSearchList(item.goods_id)">
                  <text>{{item.goods_name}}</text>
                  <uni-icons custom-prefix="iconfont" type="right" size="20"></uni-icons>
            </view>
        </view>
    </view>
    
    <view class="boxsearchItem" v-else>
        <view class="searchDelect">
          <text>搜索历史</text>
          <uni-icons type="trash-filled" size="25" @click="clearstoarge"></uni-icons>
        </view>
       
            <view class="checkboxSearch">
          <block v-for="(item,index) in searchItem " :key="index">
           <uni-tag :text="item" class="textSearch" @click="handlerGoodsList(item)"></uni-tag>
          </block>
             </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        searchItem:[],
        show:true,
        showSync:true,
        searchVal:"",
        timer:null,
        kw:"",
        searList:[]
      };
    },
    onLoad() {
      this.searchItem=JSON.parse(uni.getStorageSync("kw") || '[]')
    },
    methods:{
      handlerGoodsList(item){
         uni.navigateTo({
           url:`/subpkg/goods_list/goods_list?kw=${item}`
         })
      },
      clearstoarge(){
          this.searchItem=[]
        uni.clearStorageSync("kw",JSON.stringify(this.searchItem))
      },
      handlerSearchList(id){
          console.log(id)
            uni.navigateTo({
              url:`/subpkg/goods_detail/goods_detail?goods_id=${id}`
            })
      },
       async getSearList(){
        if(this.kw.length===0) return this.searList=[]
          const {data:res}=await uni.$http.get("/api/public/v1/goods/qsearch",{query:this.kw})
          console.log(res)
          if(res.meta.status!==200) return uni.$showmag("数据获取失败")
          this.searList=res.message
          this.theHistoricalData()
        },
        theHistoricalData(){
           this.searchItem.unshift(this.kw)
          const resultarr = new Set(this.searchItem); 
          resultarr.delete(this.kw)
          resultarr.add(this.kw)
          this.searchItem=Array.from(resultarr)
          uni.setStorageSync("kw",JSON.stringify(this.searchItem))
        },
      input(e){
         clearTimeout(this.timer)
        this.timer=setTimeout(()=>{
          this.kw=e
          this.getSearList()
          console.log(this.kw)
        },500)
      }
    },
    
    
  }
</script>

<style lang="scss">
.uni-searchbar{
  background: #c00000;
}
.searchBox{
  position: sticky;
  top: 0;
  z-index: 999;
}
.searchbox-item{
  font-size: 15px;
  .searchbox{
    margin: 10px;
    .searchList{
      padding: 10px 10px;
      border-bottom: 0.5px solid #e8e8e8;
      display: flex;
      justify-content: space-between;
      box-shadow: 10px 10px 10px #E5E5E5;
     text{
       width: 98%;
       font-size: 16px;
       display:block;
         overflow:hidden;
         word-break:keep-all;
         white-space:nowrap;
         text-overflow:ellipsis;
        margin-top: 10px;
       
     }
    }
  }
}
.searchDelect{
  display: flex;
  justify-content: space-between;

  box-shadow: 0px 0px 5px #F5F5F5;
  font-size: 15px;
  font-weight: bold;
  border-bottom: 1px solid #E5E5E5;
  
}
  .checkboxSearch{
    display: flex;
    flex-wrap: wrap;
  .textSearch{
   margin: 10px;
  }
  }
  .boxsearchItem{
     margin: 20px 10px;
  }
</style>
